#include<iostream>
#include<string>
#include<vector>
#include<pthread.h>
#include<unistd.h>

pthread_cond_t gcond = PTHREAD_COND_INITIALIZER;//条件变量
pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER;//互斥锁


void *MasterCore(void *args)
{
    sleep(3);
    std::cout<<"master 开始..." <<std::endl;
    std::string name = static_cast<const char *>(args);
    while (true)
    {
        //唤醒全部线程
        pthread_cond_broadcast(&gcond);
        //唤醒其中一个队列首部的线程
       // pthread_cond_signal(&gcond);
        std::cout<<"master 唤醒一个线程..."<<std::endl;
        sleep(1);
    }
    
}
void *SlaverCore(void *args)
{
    std::string name = static_cast<const char *>(args);
    while (true)
    {
       //加锁
       pthread_mutex_lock(&gmutex);
       //一般条件变量实在加锁和解锁之间使用的
       pthread_cond_wait(&gcond,&gmutex);
       //解锁
       std::cout<<"当前被唤醒的线程是："<<name<<std::endl;
       pthread_mutex_unlock(&gmutex);
    }
}
void StartMaster(std::vector<pthread_t> *tidsptr)
{
    //创建线程
    pthread_t tid;
    int n = pthread_create(&tid,nullptr,MasterCore,(void*)"Master thread");
    if(n==0)
    {
        std::cout<<"create master sucess"<<std::endl;
    }
    //末尾直接构造元素
    tidsptr->emplace_back(tid);
}
void StartSlaver(std::vector<pthread_t> *tidsptr,int num = 3 )
{
    for(int i = 0;i<num;++i)
    {
        char *name = new char[64];
        snprintf(name,64,"slaver-%d",i+1);
        pthread_t tid;
        int n = pthread_create(&tid,nullptr,SlaverCore,name);
        if(n==0)
        {
            std::cout<<"create sucess:"<<name<<std::endl;
            tidsptr->emplace_back(tid);
        } 
    }
}
void WaitThread(std::vector<pthread_t> &tids)
{
    for(auto &tid:tids)
    {
        pthread_join(tid,nullptr);
    }
}
int main()
{
    std::vector<pthread_t> tids;
    StartMaster(&tids);
    StartSlaver(&tids,5);
    WaitThread(tids);
    return 0;
}